package bll

import (
	"context"
	"manager/errors"

	"manager/event"
	"manager/model"
	"manager/model/entity"
	"manager/store"
	"manager/store/postgres"
	"time"
)

type batteryType struct {
	iBatteryType store.IBatteryType
}

var BatteryType = &batteryType{
	iBatteryType: postgres.BatteryType,
}

func (a *batteryType) init() func() {
	return func() {}
}

func (a *batteryType) onEvent(*event.Data) {}

// Create 创建
func (a *batteryType) Create(ctx context.Context, in *model.BatteryTypeCreateRequest) error {
	var (
		err error
	)

	// 构建创建现场数据
	c := buildBatteryType(in)
	_, err = a.iBatteryType.Create(ctx, c)
	return err
}

// Update 更新
func (a *batteryType) Update(ctx context.Context, in *model.BatteryTypeUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt

	if in.Name != nil {
		dict["name"] = in.Name
	}

	if in.Version != nil {
		dict["version"] = in.Version
	}

	if in.Voltage != nil {
		dict["voltage"] = in.Voltage
	}

	if in.Type != nil {
		dict["type"] = in.Type
	}

	if in.Supplier != nil {
		dict["supplier"] = in.Supplier
	}

	if in.Capacity != nil {
		dict["capacity"] = in.Capacity
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	return a.iBatteryType.Update(ctx, in.Id, dict)
}

// Delete 删除
func (a *batteryType) Delete(ctx context.Context, in *model.BatteryTypeDeleteRequest) error {
	// 校验电池类型是否被使用
	rsp, _ := Battery.List(ctx, &model.BatteryListRequest{
		Index:       1,
		Size:        1,
		BatteryType: &in.Id,
	})

	if rsp.Total > 0 {
		return errors.New("电池类型已被使用，无法删除")
	}

	return a.iBatteryType.Delete(ctx, in.Id)
}

// List 列表查询
func (a *batteryType) List(ctx context.Context, in *model.BatteryTypeListRequest) (*model.BatteryTypeListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.BatteryType
		out   = &model.BatteryTypeListResponse{}
	)

	if total, list, err = a.iBatteryType.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = model.BatteryTypesEntityToDto(list)

	return out, nil
}

// Find 列表查询
func (a *batteryType) Find(ctx context.Context, in *model.BatteryTypeInfoRequest) (*model.BatteryTypeInfo, error) {
	var (
		err  error
		data *entity.BatteryType
		out  = &model.BatteryTypeInfo{}
	)

	if data, err = a.iBatteryType.Find(ctx, in); err != nil {
		return nil, err
	}

	out = model.BatteryTypeEntityToDto(data)
	return out, nil
}

// IsExistBatteryType 判断是否存在
func (a *batteryType) IsExistBatteryType(batteryType int64) bool {
	info, err := a.Find(context.Background(), &model.BatteryTypeInfoRequest{Id: batteryType})
	if err != nil {
		return false
	}

	return info.Id > 0

}

func (a *batteryType) QueryBatteryTypeByCode(ctx context.Context, code string) (entity.BatteryType, error) {
	var bt entity.BatteryType
	err := postgres.GetDB(ctx).Model(&entity.BatteryType{}).Where("version=?", code).First(&bt).Error
	return bt, err
}

// buildBatteryType 构建创建数据现场
func buildBatteryType(in *model.BatteryTypeCreateRequest) *entity.BatteryType {
	// todo: check the entity is required
	return &entity.BatteryType{

		Name: in.Name,

		Version: in.Version,

		Voltage: in.Voltage,

		Type: in.Type,

		Supplier: in.Supplier,

		Capacity: in.Capacity,

		CreatedAt: time.Now().Unix(),

		UpdatedAt: time.Now().Unix(),
	}
}
